
#ifndef _MAP_H_
#define _MAP_H_

// #include <asm-generic/errno-base.h>
// #include <asm-generic/errno.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>

/*------------------------------------------------------------------------*/
#define MAP2_X_FILE	"x.MAP"						//未排序文件
#define MAP2_X_MAX_SIZE		(11*11)			//未排序文件最大大小
#define MAP2_VAL_SIZE		(sizeof(uint32_t))	//map的值大小

typedef FILE  FIL;

typedef struct _map2 {
	uint32_t flags;
	uint32_t order_file_flags;
	FIL* file;
	uint8_t klen;
	char name[8];
	char fname[8];
	char fpath[32];
}map2_t;

#pragma pack(1)

typedef struct _map2_item{
	uint8_t check_sum;
	uint8_t flag;
	uint32_t index;
	uint8_t klen;
	uint8_t kval[];
}map2_item_t;

#pragma pack()

typedef struct _select_cond{
	const void *key;
	const void *cond;
	int (*filter)(const void* c, map2_item_t* item);
	uint8_t limit;
}select_cond_t;

typedef struct _select_item{
	uint32_t index;
	int pos;
	char fnum;
	uint8_t flag;
}select_item_t;

typedef struct _select_result{
	select_item_t* items[8];
	uint8_t count;
}select_result_t;

/*============================================================================================================*/

int map2_setup(map2_t* m, const char* name, uint32_t klen);


int map2_add(map2_t* m, const void* key_data, size_t key_size, uint32_t index);

int map2_search(map2_t* m, const void* key, map2_item_t* item);

int map2_delete(map2_t* m, const void* key);

int map2_setup2(map2_t* m, const char* name, uint32_t klen);

int map2_add2(map2_t* m, const void* key_data, size_t key_size, uint32_t index);

int map2_search2(map2_t* m, const void* key, map2_item_t* item);

int map2_delete2(map2_t* m, const void* key);

int map2_clear(map2_t* m);

int map2_select(map2_t* m, select_cond_t* cond, select_result_t* result);

int map2_update(map2_t* m, char fnum, int pos, const void* key, uint32_t index, uint8_t flag);

void map2_select_result_clean(select_result_t* result);

#endif
